import axios from "axios"
import { ElMessage } from 'element-plus'
const http = axios.create({
    baseURL: '',
    timeout: 3000,
});

http.interceptors.request.use(function (config) {
    const store = JSON.parse(localStorage.getItem('userinfo'))
    if (store&&store.token) {
        config.headers['Authorization'] = 'Bearer ' + store.token
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
http.interceptors.response.use(function (response) {
    if(response.data.code!==0){
        ElMessage.error(response.data.msg)
    }
    return response.data;
}, function (error) {
    if(error.response.status===401){
        localStorage.clear()
        window.location.href = "/login"
    }
    // 对响应错误做点什么
    return Promise.reject(error);
});

export default http